<?php
function sort_targets (&$array) {
	if (count($array) == 0 || count($array) == 1) {
		return;
	}
	$n = count($array);
	
	for ($i = 0, $n2 = $n - 1; $i < $n2; $i++) {
		$min = $array[$i]->n_array;
		$cnt_min = count($min);
		$min_index = $i;
		
		for ($j = $i + 1; $j < $n; $j++) {
			$current = $array[$j]->n_array;
			$cnt_current = count($current);
			
			$found = false;
			
			if ($cnt_current <= $cnt_min) {
				$min_n = $cnt_current;
			}
			else {
				$min_n = $cnt_min;
			}
			
			for ($k = 0; $k < $min_n; $k++) {
				if ($current[$k] < $min[$k]) {
					$found = true;
					break;
				}
				elseif ($current[$k] > $min[$k]) {
					break;
				}
			}
			
			if ($k == $min_n && $cnt_current < $cnt_min) {
				$found = true;
			}
		
			if ($found) {
				$min_index = $j;
				$min = $current;
				$cnt_min = count($min);
			}
		}
		
		if ($min_index != $i) {
			$temp = $array[$i];
			$array[$i] = $array[$min_index];
			$array[$min_index] = $temp;
		}
	}
}
?>